package clothes.tools.dao.impl;

import clothes.tools.dao.CommodityDao;
import clothes.tools.pojo.Commodity;

import java.util.List;

public class CommodityDaoImpl extends BaseDao implements CommodityDao {
    @Override
    public int addCommodity(Commodity commodity) {
        String sql="insert into commodity(pid,pname,pcolor,psize,price,pnum) values(?,?,?,?,?,?)";
        return upDate(sql,commodity.getPid(),commodity.getPname(),commodity.getPcolor(),commodity.getPsize(),commodity.getPrice(),commodity.getPnum());
    }

    @Override
    public int deleteCommodityById(String id) {
        String sql="delete from commodity where pid=?";
        return upDate(sql,id);
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        String sql="update commodity set pid=?,pname=?,pcolor=?,psize=?,price=?,pnum=? where pid=?";
        return upDate(sql,commodity.getPid(),commodity.getPname(),commodity.getPcolor(),commodity.getPsize(),commodity.getPrice(),commodity.getPnum(),commodity.getPid());
    }

    @Override
    public Commodity queryCommodityById(String id) {
        String sql="select pid,pname,pcolor,psize,price,pnum from commodity where pid=?";
        return queryForOne(sql,Commodity.class,id);
    }

    @Override
    public List<Commodity> queryCommodities() {
        String sql="select pid,pname,pcolor,psize,price,pnum from commodity";
        return queryForList(sql,Commodity.class);
    }

    @Override
    public List<Commodity> queryCommodities(String page, String limit) {
        String sql="select pid,pname,pcolor,psize,price,pnum from commodity limit ?,?";
        int page1=Integer.parseInt(page);
        int limit1=Integer.parseInt(limit);
        return queryForList(sql,Commodity.class,(page1-1)*limit1,limit1);
    }

    @Override
    public List<Commodity> queryCommodity(String page, String limit,String name) {
        String sql="select pid,pname,pcolor,psize,price,pnum from commodity where pid like '%"+name+"%' or pname like '%"+name+"%' limit ?,?";
        int page1=Integer.parseInt(page);
        int limit1=Integer.parseInt(limit);
        return queryForList(sql,Commodity.class,(page1-1)*limit1,limit1);
    }

}
